package Master.interfaces;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Master.Configurations;
import Master.Resource;
import Module.StatusService.JobStatus;
import Module.StatusService.WorkerStatus;

public class WorkerPanelItem extends PanelItem {
	private JButton option_btn = new JButton();
	private JLabel icon_text=new JLabel();
	private FlashingThread flashing=null;
	private Resource this_resource;
	private WorkerStatus current_status;
	private WorkerStatus previous_status;

	public WorkerPanelItem(int id,Resource this_resource) {
		super(id);
		this.this_resource=this_resource;	
		initialiseGUI();
		
		option_btn.setText("Worker "+id);
		setStatus(WorkerStatus.active);//Default
		previous_status=WorkerStatus.active;
	}
	
	private void initialiseGUI()
	{
		this.setBorderStyle(BorderFactory.createRaisedBevelBorder());
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;		
		
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(3,3,3,3);
		this.addComponent(icon_text,c);
		
		c.gridx = 0;
		c.gridy = 1;
		this.addComponent(option_btn,c);
		
		option_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				WorkerDetailDialog d = new WorkerDetailDialog("Worker "+getID(),this_resource,current_status);
				d.appear();
			}
		});
	}
	
	public void setStatus(WorkerStatus status_code)
	{
		previous_status=current_status;
		current_status=status_code;
		
		switch(status_code)
		{
			case dead:
				startFlashing(new Color(Integer.parseInt(Configurations.W_DEAD_COLOR,16)));
				icon_text.setIcon(new ImageIcon(Configurations.W_DEAD_ICON));
				break;
			case active:
				changeColor(new Color(Integer.parseInt(Configurations.W_IDLE_COLOR,16)));
				icon_text.setIcon(new ImageIcon(Configurations.W_IDLE_ICON));
				break;
			case running:
				changeColor(new Color(Integer.parseInt(Configurations.W_RUNNING_COLOR,16)));
				icon_text.setIcon(new ImageIcon(Configurations.W_RUNNING_ICON));
				break;
		}	
	}
	
	private void changeColor(Color c)
	{
		stopFlashing();
		this.setBackgroundColor(c);
		this.setBorderColor(c);
	}
	
	private void startFlashing(Color c)
	{
		if(previous_status==current_status)
			return;
		
		this.setBorderColor(c);
		flashing = new FlashingThread(c,this);
		flashing.start();
	}
	
	private void stopFlashing()
	{
		if(flashing!=null)
			flashing.quit();
	}
	
	private class FlashingThread extends Thread
	{
		private Color c;private WorkerPanelItem p;
		private boolean run=true;
		private boolean alt=true;
		public FlashingThread(Color c,WorkerPanelItem p){this.c=c;this.p=p;}
		public void quit(){run=false;}
		public void run()
		{
			while(run){
				try{
					if(alt)
						p.setBackgroundColor(Color.WHITE);
					else
						p.setBackgroundColor(c);
					alt=!alt;
					Thread.sleep(1000);
				}catch(Exception e){}
			}
		}
	}
}
